#include "stdafx.h"
#include "FlyEngine.h"
#include "FlyRenderEngine.h"
#include "FlyScene.h"

FlyEngine::FlyEngine() : m_pCurScene( NULL )
{
}

FlyEngine::~FlyEngine()
{
	Destroy();
}

void FlyEngine::Destroy()
{
	SAFE_DELETE( m_pCurScene );
}

bool FlyEngine::Initialize()
{
	return true;
}

void FlyEngine::Tick( unsigned deltaTime )
{
	if ( IsChangeScene() )
	{
		if ( m_pCurScene )
			m_pCurScene->onExit();

		SAFE_DELETE( m_pCurScene );

		m_pCurScene = FlySceneFactoryContainer::Inst().Create( m_nextSceneType );
		if ( m_pCurScene )
			m_pCurScene->onEnter();
	}

	if ( m_pCurScene )
	{
		m_pCurScene->Tick( deltaTime );
	}

	return;
}

void FlyEngine::Render()
{
	if ( RENDERER().BeginScene() )
	{	
		// render the scene.
		if ( m_pCurScene )
		{
			m_pCurScene->Render();
		}
		
		RENDERER().EndScene();
	}
}

void FlyEngine::SetScene( std::string sceneType )
{
	m_nextSceneType = sceneType;
}

bool FlyEngine::IsChangeScene()
{
	if ( m_nextSceneType.size() <= 0 )
		return false;
	
	if ( m_pCurScene == NULL )
		return true;

	if ( m_nextSceneType == m_pCurScene->GetType() )
		return false;

	return true;
}